package leetcode_500;

import java.util.ArrayList;
import java.util.List;

/**
 *@author 周杨
 *KeyboardRow_500 判断字符串上的字符是否在键盘的一行
 *describe:用contains算法 AC 45%
 *2018年9月4日 下午7:49:53
 */
public class KeyboardRow_500 {
	 public static void main(String[] args) {
		 KeyboardRow_500 test=new KeyboardRow_500();
		 test.findWords(new String[] {"Hello","Alaska","Dad","Peace"});
	}
	
	 public String[] findWords(String[] words) {
		 List<Character> one=new ArrayList<Character>();
		 one.add('q');
		 one.add('w');
		 one.add('e');
		 one.add('r');
		 one.add('t');
		 one.add('y');
		 one.add('u');
		 one.add('i');
		 one.add('o');
		 one.add('p');
	     //String one="qwertyuiop";
		 List<Character> two=new ArrayList<Character>();
		 two.add('a');
		 two.add('s');
		 two.add('d');
		 two.add('f');
		 two.add('g');
		 two.add('h');
		 two.add('j');
		 two.add('k');
		 two.add('l');
	    // String two="asdfghjkl";
		 List<Character> three=new ArrayList<Character>();
		 three.add('z');
		 three.add('x');
		 three.add('c');
		 three.add('v');
		 three.add('b');
		 three.add('n');
		 three.add('m');
	     //String three="zxcvbnm";	
	     List<String> list=new ArrayList<String>();
	     for(String word:words) {
	    	 if(word.length()<=1) {
	    		 list.add(word);
	    		 continue;
	    	 }
	    	 String temp=word.toLowerCase();
	    	 char c=temp.charAt(0);
	    	 List<Character> judge=null;
	    	 if(one.contains(c))
	    		 judge=one;
	    	 else if(two.contains(c))
	    		 judge=two;
	    	 else
	    		 judge=three;
	    	 int index=1;
	    	 while(index<temp.length()) {
	    		 c=temp.charAt(index);
	    		 if(!judge.contains(c))
	    			 break;
	    		 ++index;
	    	 }
	    	 if(index==temp.length())
	    		 list.add(word);
	     }
	     String[] strs=new String[list.size()];
	     for(int i=0;i<strs.length;++i) {
	    	 strs[i]=list.get(i);
	     }
	     return strs;
	 }
}
